﻿#include "log_textbrowser.h"
#include <QtCore/QFileInfo>
#include <QtGui/QDesktopServices>

#include "log_helper.h"
#include "windows.h"
#include "shellapi.h"

#ifdef Q_CC_MSVC
#pragma execution_character_set("utf-8")
#endif

LogTextBrowser::LogTextBrowser(QWidget* parent /*= Q_NULLPTR*/) : QTextBrowser
(parent)
{
	//绑定信号，将调试信息输出值ui
	//connect(MyLog::Instance(), &MyLog::sigDebugStrData, this, &Log_TextBrowser::append);

	setOpenLinks(false);
	setOpenExternalLinks(false);


	setFont(QFont("宋体", 13, 3));

	connect(this, &QTextBrowser::anchorClicked, this, &LogTextBrowser::on_anchorClicked);
    
    connect(LogHelper::Instance(), &LogHelper::sigDebugHtmlData, this, &LogTextBrowser::append);

	QMetaObject::Connection con =
		connect(this, &QTextBrowser::anchorClicked, [=](const QUrl& link)
			{
				auto path = link.path();
				QFileInfo info(path);

				if (info.isDir())
					QDesktopServices::openUrl(link);
				else if (info.isFile())
					QDesktopServices::openUrl(QUrl::fromLocalFile(path));
				//ShellExecute(NULL, "open", "notepad++", path.toLocal8Bit(), "", true);
			});
}

LogTextBrowser::~LogTextBrowser()
{

}

void LogTextBrowser::append(const QString& text)
{
	QTextEdit::append(text);
}

QSize LogTextBrowser::sizeHint()
{
	return QSize(100, 200);
}

void LogTextBrowser::on_anchorClicked(const QUrl& url)
{
	//	WinExec(" notepad++.exe d:/HL/Projects/_Npic/power-link/Viewer/src/QCustomplot/qcustomplot.cpp -n1233\r", 1);

		//	return;
	QString strUrl = url.toString();
	QStringList sp = strUrl.split(' ');
	QString cmd = QString("Notpad++.exe ") + strUrl;

	//ShellExecute(NULL, cmd);
	//QDesktopServices::openUrl(strUrl);
	QString filename = sp[0];
	QString line = sp[1];

	std::wstring operate;
	//	if (info.installAuth == 0)//有管理员权限
	//		operate = QString("runas").toStdWString();
	//	else//没有管理员权限
	operate = L"open";
	std::wstring file = QString("notepad++.exe").toStdWString();//执行文件

	filename = filename.left(filename.size() - QString(filename.split("/").last()).size());//获取执行文件路径
	std::wstring path = filename.toStdWString();//执行文件路径。

	QString para = sp[0];// QString("-n") + line;//参数1
	QString para2 = QString("-n") + line;//参数2
	QString tmp = para + " " + para2;
	std::wstring lpParameters = tmp.toStdWString();//合并参数

	int ret = 0;
	ShellExecute(NULL, operate.c_str(), file.c_str(), lpParameters.c_str(), path.c_str(), 1);// SW_SHOWNORMAL);
	QString sRet;
	switch (ret)
	{
	case 0:
		sRet = QString("memory lack.");
		break;
	case 2:
		sRet = QString("filename is error.");
		break;
	case 3:
		sRet = QString("file path is error.");
		break;
	case 11:
		sRet = QString("exe is invaliable.");
		break;
	case 26:
		sRet = QString("shared error.");
		break;
	case 27:
		sRet = QString("file is error or be short.");
		break;
	case 28:
		sRet = QString("open time out.");
		break;
	case 29:
		sRet = QString("DDE task failed.");
		break;
	case 30:
		sRet = QString("undering other's DDE");
		break;
	case 31:
		sRet = QString("no linked process.");
		break;
	default:
		sRet = QString("unknow error.");
		break;
	}

	if (ret <= 32) {//返回值大于32表示ShellExecute执行成功
	}
	else {
	}

}
